//
//  MYUpatePassViewController.m
//  CarTown
//
//  Created by xionghao on 15/9/21.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "MYUpatePassViewController.h"
#import "Puser.h"
#import "UserInfoUpdateResde.h"
@interface MYUpatePassViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldtextfield;
@property (nonatomic,strong) Puser *user;
@property (weak, nonatomic) IBOutlet UITextField *newtextfield;

@end

@implementation MYUpatePassViewController
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"修改密码";
    // Do any additional setup after loading the view from its nib.
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [XHUserDeflaut setBool:NO forKey:LOGINSTATE];
    [self.navigationController.navigationController popToRootViewControllerAnimated:YES];

}

- (IBAction)okclick:(id)sender {
    
  
    if ([self checkform]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在修改";
        
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        int power=[self.user.power intValue];
        if (power<5) {
            params[@"type"]=@"person";
            params[@"user_id"]=self.user.user_id;
        }else{
        params[@"user_id"]=self.user.com_id;params[@"type"]=@"company";
        }
        params[@"passwd"]=self.oldtextfield.text;
        params[@"newpw"]=self.newtextfield.text;
        [PostUitls Post:mypassurl parameters:params success:^(id response) {
            hud.mode=MBProgressHUDModeText;
            
            UserInfoUpdateResde *resmode=[UserInfoUpdateResde resdeWithDict:response];
            

            if ([resmode.state integerValue]==400 ) {
                [hud hide:YES];
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"密码修改成功" message:@"密码修改成功请重新登陆" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
                [alert show];
            }else{
                                 hud.labelText=resmode.msg;
                [hud hide:YES afterDelay:1];
            }
            
            
        } failure:^(NSError *error) {
            hud.mode=MBProgressHUDModeText;

               hud.labelText=@"网络错误";
              [hud hide:YES afterDelay:1];
        }];
    }
    
}
-(BOOL)checkform{
    if (self.oldtextfield.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请输入旧密码";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if (self.newtextfield.text.length>0&&self.newtextfield.text.length<6) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请输入6位或以上的新密码";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    return YES;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (IBAction)hidepass:(UIButton *)sender {
    self.newtextfield.secureTextEntry=!self.newtextfield.secureTextEntry;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
